//
//  MAMessgaeCommonCell.m
//  meiang_edu
//
//  Created by iloveko8e on 2018/9/15.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAMessgaeCommonCell.h"
#import "MAActionView.h"

@interface MAMessgaeCommonCell ()

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation MAMessgaeCommonCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = PROJECT_BG_COLOR;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self ma_addSubviews];
    }
    return self;
}

- (void)updateInfoWithDict:(NSDictionary *)dict {
    NSString *imageUrl = [NSString stringWithFormat:@"%@%@", IMG_OSS, dict[@"sender_img"]];

    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
    self.titleLabel.text = dict[@"sender_name"];
    self.descLabel.text = [NSString stringWithFormat:@"发送人：%@", dict[@"sender_username"]];
    self.infoLabel.text = dict[@"message_content"];
    self.timeLabel.text = dict[@"send_time"];
}

#pragma mark - private
- (void)ma_addSubviews {
    [self.contentView addSubview:self.baseView];
    
    [self.baseView addSubview:self.iconImageView];
    [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.top.equalTo(@12);
        make.width.height.equalTo(@40);
    }];
    
    [self.baseView addSubview:self.titleLabel];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(12);
        make.top.equalTo(@12);
    }];
    
    [self.baseView addSubview:self.deleteBtn];
    [self.deleteBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-12));
        make.top.equalTo(@12);
        make.width.height.equalTo(@16);
    }];
    
    [self.baseView addSubview:self.descLabel];
    [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
    }];
    
    [self.baseView addSubview:self.infoLabel];
    [self.infoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.top.equalTo(self.descLabel.mas_bottom).offset(12);
        make.right.equalTo(@(-12));
    }];
    
    [self.baseView addSubview:self.timeLabel];
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoLabel.mas_bottom).offset(12);
        make.left.equalTo(@12);
    }];
    
    [self.baseView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@4);
        make.right.equalTo(@(-4));
        make.top.equalTo(@4);
        make.bottom.equalTo(self.timeLabel.mas_bottom).offset(12);
        make.bottom.equalTo(@0);
    }];
}

- (void)ms_tappedDeleteMessageBtn {
    !self.messageDeleteBlock ?: self.messageDeleteBlock();
}

#pragma mark - getter
- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.layer.cornerRadius = 4;
    }
    return _baseView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.backgroundColor = [UIColor redColor];
        _iconImageView.layer.cornerRadius = 20;
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc] init];
        [_deleteBtn setImage:[UIImage imageNamed:@"closebutton_logo"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(ms_tappedDeleteMessageBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.text = @"张三demo";
    }
    return _titleLabel;
}

- (UILabel *)descLabel {
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.font = [UIFont systemFontOfSize:14];
        _descLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _descLabel.textAlignment = NSTextAlignmentLeft;
        _descLabel.text = @"发送人：王五";
    }
    return _descLabel;
}

- (UILabel *)infoLabel {
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.font = [UIFont systemFontOfSize:14];
        _infoLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _infoLabel.textAlignment = NSTextAlignmentLeft;
        _infoLabel.text = @"哈哈哈哈哈哈哈哈哈哈哈或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或不不哈哈哈哈哈哈哈哈哈哈哈或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或不不哈哈哈哈哈哈哈哈哈哈哈或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或不不";
        _infoLabel.numberOfLines = 0;
    }
    return _infoLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.text = @"2018-09-09 10:10";
        _timeLabel.numberOfLines = 0;
    }
    return _timeLabel;
}

@end

